package module.edittroop.view.troopinfopanel
{
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinUI.containers.ContainerBase;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.List;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.ui.Mouse;
	
	import module.edittroop.event.EditTroopEvent;
	import module.edittroop.view.editnewtrooppanel.EditNewTroopPanel;
	import module.main.view.ResourceConst;
	

	/**
	 *英雄头像面板
	 * @author jichang
	 * 
	 */	
	public class HeroAvatarPanel extends ContainerBase
	{
		/**
		 *点击移动一格按钮 
		 */		
		private var _smallDistancBtnR:Button;
		private var _smallDistancBtnL:Button;
		/**
		 *点击移动一个面板距离大小小得按钮 
		 */		
		private var _bigDistancBtnR:Button;
		private var _bigDistancBtnL:Button;
		/**
		 *玩家头像框 
		 */		
		private var _headFarem:Class
		/**
		 *编辑新队伍按钮 
		 */		
		private var _editNewTroopBtnBg:Button;
		private var _list:List;
		private var uiComponent:UIComponent;
		public function HeroAvatarPanel()
		{
			uiComponent = new UIComponent();
			_smallDistancBtnR = new Button();
			_smallDistancBtnL = new Button();
			_bigDistancBtnR = new Button();
			_bigDistancBtnL = new Button;
			_editNewTroopBtnBg = new Button();
			addItem(uiComponent);
			initView();
		}
		/**
		 *列表组件 
		 */
		public function get list():List
		{
			return _list;
		}

		/**
		 * @private
		 */
		public function set list(value:List):void
		{
			_list = value;
		}
		private var sprite:HeadFarem = new HeadFarem(); 
		private function initView():void
		{
			_smallDistancBtnR.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_SIGLE_BUTTON) as Sprite;
			_bigDistancBtnR.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_TOTLE_MOVE_BUTTON) as Sprite;
			_smallDistancBtnL.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_TOTLE_MOVE_BUTTON_LEFT) as Sprite;
			_bigDistancBtnL.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_SIGLE_BUTTON_LEFT) as Sprite;
			_editNewTroopBtnBg.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_BLUE_BUTTON);
			/*var sprite:Sprite = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_BIG_HEAD_FRAME) as Sprite;*/
			_list = new List(HeadFarem,sprite.width,sprite.height,5);
			_list.width = sprite.width * 5 + 20;
			_list.height = sprite.height;
			
			UIManager.getPopUpManager().addEventListener(EditTroopEvent.EDIT_NEW_TROOP,editNew);
			
			
			_smallDistancBtnR.width = 18.9;
			_smallDistancBtnR.height = 25.9;
			_smallDistancBtnL.width = 18.9;
			_smallDistancBtnL.height = 25.9;
			_bigDistancBtnR.width = 18.9;
			_bigDistancBtnR.height = 25.9;
			_bigDistancBtnL.width = 18.9;
			_bigDistancBtnL.height = 25.9;
			_editNewTroopBtnBg.width = 70.3;
			_editNewTroopBtnBg.height = 52.8;
			this.width = 465.9;
			this.height = 54.0;
			setPosition();
		}
		private function editNew(e:EditTroopEvent):void
		{
			if(e.data)
			{
				_list.source.push(e.data);
				_list.refreshList(_list.source);
			}
		}
		private function setPosition():void
		{
			setSmallDistancBtnL();
			setBigDistancBtnL();
			
			setSmallDistancBtnR();
			setBigDistancBtnR();
			
			setEditNewTroopBtnBg();
			setList();
		}
		private function setEditNewTroopBtnBg():void
		{
			var $label_1:Label = new Label;
			$label_1 = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			$label_1.text = "创建部队";
			_editNewTroopBtnBg.x = _bigDistancBtnR.x + _bigDistancBtnR.width - 10;
			$label_1.x = (_editNewTroopBtnBg.width - $label_1.width) / 2;
			$label_1.y =  (_editNewTroopBtnBg.height - $label_1.height) / 2;
			uiComponent.addChild(_editNewTroopBtnBg);
			_editNewTroopBtnBg.addChild($label_1);
			_editNewTroopBtnBg.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 *设置list框位置 
		 * 
		 */		
		private function setList():void
		{
			_list.x = _smallDistancBtnL.width + 5;
			_list.addEventListener(UIEvent.LISTITEM_CLICK,aaa);
			uiComponent.addChild(_list);
		}
		private function removeFilter():void
		{
			_headFaremFitel.filters = [];
		}
		private var _headFaremFitel:HeadFarem;
		private function aaa(e:UIEvent):void
		{
			var $listItemClick:EditTroopEvent = new EditTroopEvent(EditTroopEvent.LIST_ITEM_CLICK_EVENT);
			$listItemClick.data = e.data;
			var arr:Array = _list.getListItem();
			dispatchEvent($listItemClick);
			if((e.data as HeadFarem) !=_headFaremFitel&&_headFaremFitel)
			{
				removeFilter();
			}
			_headFaremFitel = e.data as HeadFarem;
			var $fiter:GlowFilter = new GlowFilter();
			$fiter.alpha = 0.5
			var $filtersArray:Array = new Array($fiter); 
			(e.data as HeadFarem).filters = $filtersArray;
		/*	trace((e.data as HeadFarem).data.index);
			trace((e.data as HeadFarem).data.test);*/
		}
		/**
		 *设置小步向左移动按钮位置 
		 * 
		 */		
		private function setSmallDistancBtnL():void
		{
			uiComponent.addChild(_smallDistancBtnL);
			_smallDistancBtnL.addEventListener(MouseEvent.CLICK,clickHandler)
		}
		private var smallIdex:int = 0;
		private function clickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _smallDistancBtnL:
					if(smallIdex ==0)
					{
						return;
					}
					if(list.source == null)
					{
						return;
					}
					smallIdex = smallIdex - sprite.width - 5;
					_list.rollTime(smallIdex,0,100);
					break;
				case _smallDistancBtnR:
					if(list.source == null)
					{
						return;
					}
					if(smallIdex == (_list.source.length - 1) * (sprite.width + 5))
						return;
					smallIdex = smallIdex + sprite.width + 5;
					_list.rollTime(smallIdex,0,100);
					break;
				case _bigDistancBtnL:
					if(list.source == null)
					{
						return;
					}
					if(smallIdex == 0)
						return;
					if((smallIdex - 5) / _list.width < 1)
					{
						_list.rollTime(0,0,(smallIdex / sprite.width) * 100);
						smallIdex = 0;
						_bigDistancBtnL.mouseChildren = false;
						_bigDistancBtnL.mouseEnabled = false;
					}
					else
					{
						smallIdex = smallIdex - _list.width - 5;
						_list.rollTime(smallIdex,0,500);
					}
					break;
				case _bigDistancBtnR:
					if(list.source == null)
					{
						return;
					}
					if((smallIdex + _list.width) > (sprite.width *  (_list.source.length - 1) + 5 *  (_list.source.length - 1)))
						{
							_list.rollTime(sprite.width *  (_list.source.length - 1) + 5 * ( (_list.source.length - 1)),0,( (_list.source.length - 1) - smallIdex / (sprite.width + 5)) * 100);
							smallIdex = sprite.width *  (_list.source.length - 1) + 5 * ( (_list.source.length - 1));
						}
					else
					{
						smallIdex = smallIdex + _list.width + 5;
						_list.rollTime(smallIdex,0,500);
					}
					break;
				case _editNewTroopBtnBg:
					var getNewTroopPanel:EditTroopEvent = new EditTroopEvent(EditTroopEvent.GET_NEW_TROOP_PANEL);
					dispatchEvent(getNewTroopPanel);
					break;
				default:
					break;
			}
		}
		/**
		 * 设置大步向右移动按钮位置
		 * 
		 */		
		private function setBigDistancBtnL():void
		{
			_bigDistancBtnL.y = _bigDistancBtnL.height;
			uiComponent.addChild(_bigDistancBtnL);
			_bigDistancBtnL.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 *设置小步向右移动按钮位置 
		 * 
		 */		
		private function setSmallDistancBtnR():void
		{
			_smallDistancBtnR.x = _list.x + _list.width + 28;
			uiComponent.addChild(_smallDistancBtnR);
			_smallDistancBtnR.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 *设置小步向右移动按钮位置 
		 * 
		 */		
		private function setBigDistancBtnR():void
		{
			_bigDistancBtnR.x = _list.x + _list.width + 48;
			_bigDistancBtnR.y = _bigDistancBtnL.y;
			uiComponent.addChild(_bigDistancBtnR);
			_bigDistancBtnR.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		public function changeView():void
		{
			
		}
	}
}